package com.brother.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.brother.Language;
import com.brother.ibatis.ClientFactory;
import com.brother.model.RealTimeData;
import com.brother.model.Resource;
import com.brother.model.Tip;
import com.brother.model.Tracker;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * @author ling
 *
 */
public class TipService {
public void addTip(Tip tip ){
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		map.insert("insertTip", tip);
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
}

public List<Tip> findByExample(Tip tip){
	List<Tip> result = new ArrayList();
	SqlMapClient map = ClientFactory.getSqlMapper();
	try {
		result = map.queryForList("selectTipByExample", tip);
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	return result;
}

/**返回所有tracker的最近的两个tip
 * @param trackers
 * @return
 */
public List<Tip> getTrackerLastTip(List<Tracker> trackers,String lang){
	List<Tip> result = new ArrayList();
	SqlMapClient map = ClientFactory.getSqlMapper();
	Resource r = new Resource(); 
	if(lang == null){
		lang = "en";
	}
	r.setLanguage(lang.toString());
	try {
		for(Tracker t : trackers){
			Tip tip = new Tip();
			tip.setResource(r);
			tip.setTrackerid(t.getTrackerid());
			tip.setOrderCondition("order by createtime desc");
			tip.setTopCondition("limit 0,2");
			List tips = map.queryForList("selectTipByExample", tip);
			result.addAll(tips);
		}
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	return result;
}


/**将当前tracker的连接情况插入数据库中
 * @param userId
 */
public void addUserTrackerTips(String userId){
	SqlMapClient map = ClientFactory.getSqlMapper();
	TrackerService tks = new TrackerService();
	RealDataService rds = new RealDataService();
	List<Tracker> trackers = tks.findUserTracker(Integer.parseInt(userId));
	List<RealTimeData> realtimedatas = new ArrayList();
	try {
		for(Tracker t : trackers){
			Tip tip = new Tip();
			
			tip.setTrackerid(t.getTrackerid());
			
			boolean isConnected  = rds.isConnected(t.getTrackerid(), realtimedatas);
			if(!isConnected){
				if(realtimedatas.size() > 0){
					tip.setCreateTime(realtimedatas.get(0).getTraceDateTime());
				}else{
					tip.setCreateTime(new Date());
				}
				tip.setKeyName(Tip.KeyNames.Tracker_not_connect_to_Server.toString());
				map.insert("insertTip", tip);
			}
			realtimedatas.clear();
		}
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
}

/**
 * @param userTrackerId  用户输入的唯一标志码，一般为手机号码
 */
public void addTip(String userTrackerId, Tip.KeyNames tipType){
	TrackerService tks = new TrackerService();
	Tracker tracker = tks.getTrackerByFieldName("userTrackerId", userTrackerId);
	SqlMapClient map = ClientFactory.getSqlMapper();
	
	Tip tip = new Tip();
	tip.setTrackerid(tracker.getTrackerid());
	tip.setCreateTime(new Date());
	tip.setKeyName(tipType.toString());
	try {
		map.insert("insertTip", tip);
	} catch (SQLException e) {
		Logger.getLogger(this.getClass()).error(e);
	}
	
}

}
